<html>
<head><meta charset="utf-8"><title>semantic difference between move vs `mut` arg · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html">semantic difference between move vs `mut` arg</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213738241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213738241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213738241">(Oct 19 2020 at 02:36)</a>:</h4>
<p>Is there any semantic difference between this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">new</span><span class="p">.</span><span class="n">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">new</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>and this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>? I can't think of any difference, but I wanted to check :)</p>



<a name="213743156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213743156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213743156">(Oct 19 2020 at 04:49)</a>:</h4>
<p>You move <code>self</code> into <code>foo</code> when you call it. So no, there should not be a difference.</p>



<a name="213775526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775526">(Oct 19 2020 at 12:10)</a>:</h4>
<p>And note that the mut won’t show up in rustdoc either.</p>



<a name="213775642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775642">(Oct 19 2020 at 12:11)</a>:</h4>
<p>What are the rules for what shows up in the rustdoc? I know that pattern matching in parameters shows up, which I always thought was a little weird</p>



<a name="213775762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775762">(Oct 19 2020 at 12:12)</a>:</h4>
<p>That seems like a bug if that's true... I would expect the public signature of the function to be what's shown in rustdoc and nothing more</p>



<a name="213775828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775828">(Oct 19 2020 at 12:13)</a>:</h4>
<p>well it makes a tiny bit of sense because what parameter name should it show?</p>



<a name="213775860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775860">(Oct 19 2020 at 12:13)</a>:</h4>
<p>true</p>



<a name="213775872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213775872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213775872">(Oct 19 2020 at 12:13)</a>:</h4>
<p>the parameter name is of course some (desirable) leakage of information beyond just the signature</p>



<a name="213776250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776250">(Oct 19 2020 at 12:18)</a>:</h4>
<p>FWIW I would be open to removing the <code>mut</code> in rustdoc for this</p>



<a name="213776302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776302">(Oct 19 2020 at 12:18)</a>:</h4>
<p>It doesn't seem very useful to document</p>



<a name="213776340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776340">(Oct 19 2020 at 12:18)</a>:</h4>
<p>Jake is saying it isn't in rustdoc now though</p>



<a name="213776415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776415">(Oct 19 2020 at 12:19)</a>:</h4>
<p>there isn't really a workaround for pattern matching in parameters though and still getting nice docs except for just not matching in parameters</p>



<a name="213776436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776436">(Oct 19 2020 at 12:20)</a>:</h4>
<p>which sucks because it's a nice feature</p>



<a name="213776507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776507">(Oct 19 2020 at 12:20)</a>:</h4>
<blockquote>
<p>And note that the mut won’t show up in rustdoc either.</p>
</blockquote>



<a name="213776523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213776523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213776523">(Oct 19 2020 at 12:20)</a>:</h4>
<p>Ah yup you're right</p>



<a name="213868064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213868064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213868064">(Oct 20 2020 at 01:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg/near/213776415">said</a>:</p>
<blockquote>
<p>there isn't really a workaround for pattern matching in parameters though and still getting nice docs except for just not matching in parameters</p>
</blockquote>
<p>I've sometimes wondered if <code>foo @ pattern</code> would work as a way to give a name.</p>



<a name="213869876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213869876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213869876">(Oct 20 2020 at 02:38)</a>:</h4>
<p>That seems like the leading contender in terms of syntax, but that's going to cause a problem for the borrow checker unless you can somehow indicate that the <code>foo</code> part isn't going to get used in the function</p>



<a name="213895024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/semantic%20difference%20between%20move%20vs%20%60mut%60%20arg/near/213895024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/semantic.20difference.20between.20move.20vs.20.60mut.60.20arg.html#213895024">(Oct 20 2020 at 09:39)</a>:</h4>
<p>In that regard , <code>foo @ pattern</code>, ought to treated as binding to <code>foo</code> first, and then immediately destructuring it as a <code>pattern</code>. So, if the <code>pattern</code> has moved then <code>foo</code> is unusable; else if some part is exclusively borrowed, then <code>foo</code> is unusable for the duration of the borrow; and if only shared borrows remain, <code>foo</code> can still be inspected through shared accesses. <br>
The only confusing part when the semantics of pattern-matching are not 100% clear would be that <code>mut copyable @ foo</code> would lead to <code>copyable</code> still being usable, with <code>foo</code> being a <code>Copy</code> of it.<br>
A proc-macro would already be able to express this behavior, for instance.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>